<template> 
    <el-card class="form-container" shadow="never">
        <el-form :model="shopSupplier" :rules="rules" ref="ShopSupplierFrom" label-width="150px">


                   <!-- 
                        <el-form-item
                            label="供应商ID"
                            prop="id">
                        <el-input v-model="shopSupplier.id" style="width: 370px;"/>
                    </el-form-item>

                   -->

                    <el-form-item
                            label="供应商名称"
                            prop="name">
                        <el-input v-model="shopSupplier.name" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="供应商地址"
                            prop="address">
                        <el-input v-model="shopSupplier.address" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="联系人"
                            prop="contacts">
                        <el-input v-model="shopSupplier.contacts" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="联系电话"
                            prop="contactsTel">
                        <el-input v-model="shopSupplier.contactsTel" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="开户行"
                            prop="accountBank">
                        <el-input v-model="shopSupplier.accountBank" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="开户账号"
                            prop="accountNumber">
                        <el-input v-model="shopSupplier.accountNumber" style="width: 370px;"/>
                    </el-form-item>


                   <!-- 
                        <el-form-item
                            label="创建者"
                            prop="createBy">
                        <el-input v-model="shopSupplier.createBy" style="width: 370px;"/>
                    </el-form-item>


                    <el-form-item
                            label="创建时间"
                            prop="createTime">
                        <el-input v-model="shopSupplier.createTime" style="width: 370px;"/>
                    </el-form-item>
                   -->



                    <el-form-item
                           v-if="isEdit"
                            label="是否删除"
                            prop="delFlag">
                            <el-radio-group v-model="shopSupplier.delFlag">
                                <el-radio :label="0">否</el-radio>
                                <el-radio :label="1">是</el-radio>
                                </el-radio-group>
                    </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="onSubmit('ShopSupplierFrom')">提交</el-button>
                <el-button v-if="!isEdit" @click="resetForm('ShopSupplierFrom')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script>
    import {createShopSupplier, getShopSupplier, updateShopSupplier} from '@/api/psm/shopSupplier'
    import SingleUpload from '@/components/Upload/singleUpload'

    const defaultShopSupplier = {
        name: ''
    };
    export default {
        name: 'ShopSupplierDetail',
        components: {SingleUpload},
        props: {
            isEdit: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
            shopSupplier:
            Object.assign({},
        defaultShopSupplier),
            rules: {
                name: [
                    {required: true, message: '请输入供应商名称', trigger: 'blur'},
                    {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
                ],
                    logo
            :
                [
                    {required: true, message: '请输入品牌logo', trigger: 'blur'}
                ],
                    sort
            :
                [
                    {type: 'number', message: '排序必须为数字'}
                ],
            }
        }
        },
        created() {
            if (this.isEdit) {
                getShopSupplier(this.$route.query.id).then(response => {
                    this.shopSupplier = response.data;
            })
                ;
            } else {
                this.shopSupplier = Object.assign({},
            defaultShopSupplier)
                ;
            }
        },
        methods: {
            onSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                    if(valid) {
                        this.$confirm('是否提交数据', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            if(this.isEdit
                    )
                        {
                            updateShopSupplier(this.$route.query.id, this.shopSupplier).then(response => {
                                if(response.code == 200
                        )
                            {
                                this.$refs[formName].resetFields();
                                this.$message({
                                    message: '修改成功',
                                    type: 'success',
                                    duration: 1000
                                });
                                this.$router.back();
                            }
                        else
                            {
                                this.$message({
                                    message: response.msg,
                                    type: 'error',
                                    duration: 1000
                                });
                            }

                        })
                            ;
                        }
                    else
                        {
                            createShopSupplier(this.shopSupplier).then(response => {
                                if(response.code == 200
                        )
                            {
                                this.$refs[formName].resetFields();
                                this.shopSupplier = Object.assign({},
                            defaultShopSupplier)
                                ;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success',
                                    duration: 1000
                                });
                                this.$router.back();
                            }
                        else
                            {
                                this.$message({
                                    message: response.msg,
                                    type: 'error',
                                    duration: 1000
                                });
                            }

                        })
                            ;
                        }
                    })
                        ;

                    } else {
                        this.$message({
                            message: '验证失败',
                            type: 'error',
                            duration: 1000
                        });
                return false;
            }
            })
                ;
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
                this.shopSupplier = Object.assign({},
            defaultShopSupplier)
                ;
            }
        }
    }
</script>
<style>
</style>


